@extends('layout.app')

@section('content')

    <div class="box-body">

        <table id="rowed3"></table>
        <div id="prowed3"></div>

    </div>
<script>

$(document).ready(function() {

	var data = {
        url :"/user",
        postData: postData(),
        colNames: ['用户ID' , '昵称' , '头像' , '¥ 余额' ,/* '姓名' , '银行' , '银行卡号' , */'手机号', '邀请者ID' , '是否伙伴' , '充值' , '提款' , '抢购总费用' , '邀请人数' ,  '总佣金' , '注册时间' , '操作'],
        colModel: [{
            name: 'users.id',
            key: true
        }, {
            name: 'users.nickname',
            sortable: false
        }, {
            name: 'avatar',
            sortable: false,
            formatter: function(cellvalue) {
                return '<img src="'+cellvalue+'" width="25" height="25"/>';
            }
        }, {
            name: 'users.money',
            sortable: true,
            formatter: function(cellvalue, options, object) {
                @if ($isChaoZongdai)
                return cellvalue / 100;
                @else
                return '<a href="javascript:;" onclick="addMoney('+object['users.id']+',\''+object['users.nickname']+'\')" data-toggle="modal" data-target="#addModal"><span style="font-weight:bold;">'+cellvalue/100+'</span></a>';
                @endif
            }
        },/* {
            name: 'user_name',
            sortable: false
        }, {
            name: 'bank_name',
            sortable: false
        }, {
            name: 'card_number',
            sortable: false
        },*/ {
            name: 'users.phone',
            sortable: false
        }, {
            name: 'users.bindid',
            formatter: function(cellvalue) {
                return '<a href="/user?user_id='+cellvalue+'">'+cellvalue+'</a>';
            }
        }, {
            name: 'users.id',
            sortable: false,
            search : false,
            formatter: function(cellvalue, options, object) {
                if (object.zongdai == cellvalue) {
                    return '<i class="fa fa-check text-success"></i>';
                }
                return '';
            }
        }, {
            name: '{{$pmoneyColumn}}',
            formatter: function(cellvalue, options, object) {
                return '<a href="/order/wx?status=2&user_id='+object["users.id"]+'">'+cellvalue/100+'</a>';
            }
        }, {
            name: '{{$tikuanColumn}}',
            formatter: function(cellvalue, options, object) {
                return '<a href="/user/record?type=3&user_id='+object["users.id"]+'">'+cellvalue/100+'</a>';
            }
        }, {
            name: 'smoney',
            sortable: false,
            search : false,
            formatter: function(cellvalue, options, object) {
                return '<a href="/order/order?user_id='+object["users.id"]+'">'+cellvalue+'</a>';
            }
        }, {
            name: '{{$countColumn}}',
            formatter: function(cellvalue, options, object) {
                return '<a href="/user?bindid='+object["users.id"]+'">'+cellvalue+'</a>';
            }
        }, {
            name: 'users.income',
            formatter: function(cellvalue) {
                return cellvalue / 100;
            }
        }, {
            name: 'users.reg_time',
            formatter:'date',
            formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'},
            searchoptions: {
                sopt: ['lt', 'le', 'gt', 'ge'],
                dataInit : function (elem) {
                    $(elem).datepicker({
                        dateFormat: 'yy-mm-dd',
                        monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
                        dayNamesMin: ['日','一','二','三','四','五','六'] });
                }
            }
        }, {
            name: "users.id",
            sortable: false,
            search : false,
            formatter: function(cellvalue, options, object) {
                var str = "";
                if (object.zongdai != object["users.id"]) {
                    @if ($isChaoZongdai)
                    str = '<a href="javascript:;" onclick="upDaili('+cellvalue+',\''+object["users.nickname"]+'\')" data-toggle="modal" data-target="#upDaili" title="提升为伙伴"><i class="fa fa-level-up text-success"></i></a>';
                    @endif
                }
//                 return  str + ' <a href="javascript:;" onclick="passwd('+cellvalue+', \''+object["users.nickname"]+'\')" data-toggle="modal" data-target="#pwModal" data-placement="bottom" title="清除交易密码"><i class="fa fa-key text-danger" style="width:18px;"></i></a>';
					return str;
                }
        }],
        caption: '<i class="fa fa-users"></i> {{$label2}}',
        editurl: "/user/edit",
        sortname: 'users.id',
        sortorder: "desc",
        autowidth: true
    };

	jqGrid(data, false);
});

function postData() {
    return {
        @if($request->get("bindid"))
        bindid: '{{$request->get("bindid")}}',
        @endif
        @if($request->get("zongdai"))
        zongdai: '{{$request->get("zongdai")}}',
        @endif
        @if($request->get("chao_zongdai"))
        chao_zongdai: '{{$request->get("chao_zongdai")}}',
        @endif
        @if($request->get("user_id"))
        user_id: '{{$request->get("user_id")}}',
        @endif
    }
}

function passwd(id, name) {
    $("#clearPwdSpan").html("确定清除 <span class='text-info'>" + name + "</span> 的交易密码?");
    $("#clear-id").val(id);
}

function postClearPwd(id) {
    $("#pwModal").modal('hide');
    $.post("/user/passwd/clear",{id: id}, function(result) {
        if (result.success) {
            $("#rowed3").trigger("reloadGrid");
            $.gritter.add({
                text:'清除密码成功',
                sticky: false,
                time: '',
                class_name: 'gritter-light'});

        } else {
            $.gritter.add({
                title: '错误信息',
                text:'操作失败',
                sticky: false,
                time: ''});
        }
    });
}

function upDaili(id, name) {

    $("#upDailiSpan").html("请设置 [" + name + "] 的分成百分比");
    $("#upDailiInput").val(id);
}

function postUpZongdai() {
    $("#upDaili").modal('hide');
    var id = $("#upDailiInput").val();
    var zong_ratio = $("input[name=zong_ratio]").val();
    $.post("/user/up",{id: id, zong_ratio: zong_ratio}, function(result) {
        if (result.success) {
            $("#rowed3").trigger("reloadGrid");
            $.gritter.add({
                text:'提升成功',
                sticky: false,
                time: '',
                class_name: 'gritter-light'});

        } else {
            $.gritter.add({
                title : '错误信息',
                text  : result.message,
                sticky: false,
                time: ''});
        }
    });
}

function transfer(id, money) {
    if(money > 99999999){
        $("#notice1").show();

    }else{
        $("#addModal").modal('hide');
        $("#notice1").hide();
        $.post("/user/addMoney",{id: id, money: money}, function(result){
            if (result.success) {
                $("#rowed3").trigger("reloadGrid");
                $.gritter.add({
                    text:'转账成功',
                    sticky: false,
                    time: '',
                    class_name: 'gritter-light'});

            } else {
                $.gritter.add({
                    title: '错误信息',
                    text:'操作失败',
                    sticky: false,
                    time: ''});
            }
        }).error(function(err){
            $.gritter.add({
                title:  '错误信息',
                text:   err,
                sticky: false,
                time:   ''});
        });
    }
}

function addMoney(id, name) {
    $("#notice").hide();
    $("#addModal").modal([]);
    $("#addModalLabel").html("向[<span class='text-danger'>" + name + "</span>]转账");
    $("#money_id").val(id);
    $("#add_money").val('');
}

</script>
    <div class="modal fade" id="pwModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="pwModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <h4 class="modal-title"><span id="clearPwdSpan" class="text-danger"><i class="fa fa-key"></i></span></h4>
                    <form method="post" action="/user/passwd/clear">
                        {!! csrf_field() !!}
                        <input type="hidden" id="clear-id" name="id" />
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success" onclick="postClearPwd($('#clear-id').val())"><i class="fa fa-check"></i> 确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> 取消</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="upDaili" tabindex="-1" role="dialog" aria-labelledby="upDailiModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"><span id="upDailiSpan" class="text-danger"></span></h4>
                </div>
                <div class="modal-body">
                    <form>
                        {!! csrf_field() !!}
                        <input type="hidden" id="upDailiInput" name="id" />
                        <label>填写 0 ~ 100</label>
                        <input type="number" name="zong_ratio" class="form-control"/>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success" onclick="postUpZongdai();return false;"><i class="fa fa-check"></i> 确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> 取消</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="addModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-name="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="addModalLabel"></h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="money_id" />
                <div class="form-group">
                    <label> 转账数量 </label>
                    <input id="add_money" class="form-control" type="number" />
                </div>
                <label id="notice1" class="text-danger" style="display:none;">转账数量有误,请重新输入</label>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="transfer($('#money_id').val(),$('#add_money').val())"><i class="fa fa-lock"> 确定</i></button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
@endsection